<template>
  <div class="attr-info">
    <el-dialog
      :title="info.type == 'add' ? '新增菜单' : '编辑菜单'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="属性名" prop="specsname">
            <el-input v-model="ruleForm.specsname"></el-input>
          </el-form-item>
          <el-form-item label="属性值">
            <div v-for="(item, index) in attrList" :key="index">
              <el-input class="attr-input" v-model="attrList[index]"></el-input>
              <el-button v-if="index" type="danger" @click="del(index)"
                >删除属性</el-button
              >
              <el-button v-else type="success" @click="add">新增属性</el-button>
            </div>
          </el-form-item>
          <el-form-item label="状态" prop="menus">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="info.isShow = false">取 消</el-button>
        <el-button type="primary" @click="info.isShow = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { specsAdd,specsEdit} from "@/api.js";
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      //属性值对应的数组
      attrList: [""],
      rules: {
        username: [
          { required: true, message: "请输入菜单标题", trigger: "blur" },
        ],
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  methods: {
    //增加一项
    add(){
      if(this.attrList.length<5){
        this.attrList.push("");
      }else{
        this.$message({
          type: "warning",
          message: '属性值最多5个',
        });
      }
    },
    //删除属性的一项
    del(index){
      this.attrList.splice(index,1)
      console.log(index);
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.ruleForm.attrs=this.attrList.join(',')
          if (this.info.type === "add") {
            let res = await specsAdd(this.ruleForm);
          } else {
            let res = await specsEdit(this.ruleForm);
          }
          this.info.isShow = false;
          this.$emit("update");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //给表单复制
    setValue(info) {
      console.log(info);
      this.ruleForm = { ...info };
      // this.attrList=[...this.ruleForm.attrs]
      if(info.attrs.length!=0){
        this.attrList=info.attrs
      }else{
        this.attrList=[""]
      }
      
    },
  },
};
</script>
<style>
.attr-info .attr-input {
  width: 200px;
  margin-right: 20px;
  margin-top: 10px;
}
</style>